home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVBMP.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  8KB  |  265 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevbmp.c */
  20. /* .BMP file format devices for Ghostscript */
  21. #include "gdevprn.h"
  22. #include "gdevpccm.h"
  23.  
  24. /* ------ The device descriptors ------ */
  25.  
  26. /*
  27.  * Default X and Y resolution.
  28.  */
  29. #define X_DPI 72
  30. #define Y_DPI 72
  31.  
  32. private dev_proc_print_page(bmp_print_page);
  33.  
  34. /* Monochrome. */
  35.  
  36. gx_device_printer far_data gs_bmpmono_device =
  37.   prn_device(prn_std_procs, "bmpmono",
  38.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  39.     X_DPI, Y_DPI,
  40.     0,0,0,0,            /* margins */
  41.     1, bmp_print_page);
  42.  
  43. /* 4-bit planar (EGA/VGA-style) color. */
  44.  
  45. private gx_device_procs bmp16_procs =
  46.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  47.     pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  48. gx_device_printer far_data gs_bmp16_device =
  49.   prn_device(bmp16_procs, "bmp16",
  50.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  51.     X_DPI, Y_DPI,
  52.     0,0,0,0,            /* margins */
  53.     4, bmp_print_page);
  54.  
  55. /* 8-bit (SuperVGA-style) color. */
  56. /* (Uses a fixed palette of 3,3,2 bits.) */
  57.  
  58. private gx_device_procs bmp256_procs =
  59.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  60.     pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  61. gx_device_printer far_data gs_bmp256_device =
  62.   prn_device(bmp256_procs, "bmp256",
  63.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  64.     X_DPI, Y_DPI,
  65.     0,0,0,0,            /* margins */
  66.     8, bmp_print_page);
  67.  
  68. /* 24-bit color. */
  69.  
  70. private dev_proc_map_rgb_color(map_16m_rgb_color);
  71. private dev_proc_map_color_rgb(map_16m_color_rgb);
  72. private gx_device_procs bmp16m_procs =
  73.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  74.     map_16m_rgb_color, map_16m_color_rgb);
  75. gx_device_printer far_data gs_bmp16m_device =
  76.   prn_device(bmp16m_procs, "bmp16m",
  77.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  78.     X_DPI, Y_DPI,
  79.     0,0,0,0,            /* margins */
  80.     24, bmp_print_page);
  81.  
  82. /* ------ Private definitions ------ */
  83.  
  84. /* All multi-byte quantities are stored LSB-first! */
  85. typedef ushort word;
  86. #if arch_sizeof_int == 4
  87. typedef uint dword;
  88. #else
  89. #  if arch_sizeof_long == 4
  90. typedef ulong dword;
  91. #  endif
  92. #endif
  93. #if arch_is_big_endian
  94. #  define assign_word(a,v) a = ((v) >> 8) + ((v) << 8)
  95. #  define assign_dword(a,v)\
  96.      a = ((v) >> 24) + (((v) >> 8) & 0xff00L) +\
  97.      (((dword)(v) << 8) & 0xff0000L) + ((dword)(v) << 24)
  98. #else
  99. #  define assign_word(a,v) a = (v)
  100. #  define assign_dword(a,v) a = (v)
  101. #endif
  102.  
  103. typedef struct bmp_file_header_s {
  104.  
  105.     /* BITMAPFILEHEADER */
  106.  
  107.     /*
  108.      * This structure actually begins with two bytes
  109.      * containing the characters 'BM', but we must omit them,
  110.      * because some compilers would insert padding to force
  111.      * the size member to a 32- or 64-bit boundary.
  112.      */
  113.  
  114.     /*byte    typeB, typeM;*/    /* always 'BM' */
  115.     dword    size;        /* total size of file */
  116.     word    reserved1;
  117.     word    reserved2;
  118.     dword    offBits;    /* offset of bits from start of file */
  119.     
  120. } bmp_file_header;
  121. #define sizeof_bmp_file_header (2 + sizeof(bmp_file_header))
  122.  
  123. typedef struct bmp_info_header_s {
  124.  
  125.     /* BITMAPINFOHEADER */
  126.  
  127.     dword    size;        /* size of info header in bytes */
  128.     dword    width;        /* width in pixels */
  129.     dword    height;        /* height in pixels */
  130.     word    planes;        /* # of planes, always 1 */
  131.     word    bitCount;    /* bits per pixel */
  132.     dword    compression;    /* compression scheme, always 0 */
  133.     dword    sizeImage;    /* size of bits */
  134.     dword    xPelsPerMeter;    /* X pixels per meter */
  135.     dword    yPelsPerMeter;    /* Y pixels per meter */
  136.     dword    clrUsed;    /* # of colors used */
  137.     dword    clrImportant;    /* # of important colors */
  138.  
  139.     /* This is followed by (1 << bitCount) bmp_quad structures, */
  140.     /* unless bitCount == 24. */
  141.  
  142. } bmp_info_header;
  143.  
  144. typedef struct bmp_quad_s {
  145.  
  146.     /* RGBQUAD */
  147.  
  148.     byte    blue, green, red, reserved;
  149.  
  150. } bmp_quad;
  151.  
  152. /* Write out a page in BMP format. */
  153. /* This routine is used for all formats. */
  154. private int
  155. bmp_print_page(gx_device_printer *pdev, FILE *file)
  156. {    int raster = gdev_prn_raster(pdev);
  157.     /* BMP scan lines are padded to 32 bits. */
  158.     ulong bmp_raster = raster + (-raster & 3);
  159.     int height = pdev->height;
  160.     int depth = pdev->color_info.depth;
  161.     int quads = (depth <= 8 ? sizeof(bmp_quad) << depth : 0);
  162.     byte *row = (byte *)gs_malloc(bmp_raster, 1, "bmp file buffer");
  163.     int y;
  164.     int code = 0;            /* return code */
  165.  
  166.     if ( row == 0 )            /* can't allocate row buffer */
  167.         return_error(gs_error_VMerror);
  168.  
  169.     /* Write the file header. */
  170.  
  171.     fputc('B', file);
  172.     fputc('M', file);
  173.     {    bmp_file_header fhdr;
  174.         assign_dword(fhdr.size,
  175.             sizeof_bmp_file_header +
  176.             sizeof(bmp_info_header) + quads +
  177.             bmp_raster * height);
  178.         assign_word(fhdr.reserved1, 0);
  179.         assign_word(fhdr.reserved2, 0);
  180.         assign_dword(fhdr.offBits,
  181.             sizeof_bmp_file_header +
  182.             sizeof(bmp_info_header) + quads);
  183.         if ( fwrite((const char *)&fhdr, 1, sizeof(fhdr), file) != sizeof(fhdr) )
  184.         {    code = gs_error_ioerror;
  185.             goto bmp_done;
  186.         }
  187.     }
  188.     
  189.     /* Write the info header. */
  190.  
  191.     {    bmp_info_header ihdr;
  192.         assign_dword(ihdr.size, sizeof(ihdr));
  193.         assign_dword(ihdr.width, pdev->width);
  194.         assign_dword(ihdr.height, height);
  195.         assign_word(ihdr.planes, 1);
  196.         assign_word(ihdr.bitCount, depth);
  197.         assign_dword(ihdr.compression, 0);
  198.         assign_dword(ihdr.sizeImage, bmp_raster * height);
  199.         /* Even though we could compute the resolution correctly, */
  200.         /* the convention seems to be to leave it unspecified. */
  201.         assign_dword(ihdr.xPelsPerMeter, 0);
  202.             /*(dword)(pdev->x_pixels_per_inch * (1000.0 / 30.48)));*/
  203.         assign_dword(ihdr.yPelsPerMeter, 0);
  204.             /*(dword)(pdev->y_pixels_per_inch * (1000.0 / 30.48)));*/
  205.         assign_dword(ihdr.clrUsed, 0);
  206.         assign_dword(ihdr.clrImportant, 0);
  207.         if ( fwrite((const char *)&ihdr, 1, sizeof(ihdr), file) != sizeof(ihdr) )
  208.         {    code = gs_error_ioerror;
  209.             goto bmp_done;
  210.         }
  211.     }
  212.  
  213.     /* Write the palette. */
  214.  
  215.     if ( depth <= 8 )
  216.     {    int i;
  217.         gx_color_value rgb[3];
  218.         bmp_quad quad;
  219.         quad.reserved = 0;
  220.         for ( i = 0; i != 1 << depth; i++ )
  221.         {    (*pdev->procs->map_color_rgb)((gx_device *)pdev,
  222.                 (gx_color_index)i, rgb);
  223.             quad.red = gx_color_value_to_byte(rgb[0]);
  224.             quad.green = gx_color_value_to_byte(rgb[1]);
  225.             quad.blue = gx_color_value_to_byte(rgb[2]);
  226.             fwrite((const char *)&quad, sizeof(quad), 1, file);
  227.         }
  228.     }
  229.  
  230.     /* Write the contents of the image. */
  231.     /* BMP files want the image in bottom-to-top order! */
  232.  
  233.     for ( y = height; y >= 0; y-- )
  234.     {    gdev_prn_copy_scan_lines(pdev, y, row, raster);
  235.         fwrite((const char *)row, bmp_raster, 1, file);
  236.     }
  237.  
  238. bmp_done:
  239.     gs_free((char *)row, bmp_raster, 1, "bmp file buffer");
  240.  
  241.     return code;
  242. }
  243.  
  244. /* 24-bit color mappers (taken from gdevmem2.c). */
  245. /* Note that Windows expects RGB values in the order B,G,R. */
  246.  
  247. /* Map a r-g-b color to a color index. */
  248. private gx_color_index
  249. map_16m_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  250.   gx_color_value b)
  251. {    return gx_color_value_to_byte(r) +
  252.            ((uint)gx_color_value_to_byte(g) << 8) +
  253.            ((ulong)gx_color_value_to_byte(b) << 16);
  254. }
  255.  
  256. /* Map a color index to a r-g-b color. */
  257. private int
  258. map_16m_color_rgb(gx_device *dev, gx_color_index color,
  259.   gx_color_value prgb[3])
  260. {    prgb[2] = gx_color_value_from_byte(color >> 16);
  261.     prgb[1] = gx_color_value_from_byte((color >> 8) & 0xff);
  262.     prgb[0] = gx_color_value_from_byte(color & 0xff);
  263.     return 0;
  264. }
  265.